<template>
    <Modal :mask-closable="false" v-model="toolMaintenanceVisible" :title="isAdd ? '添加设备位置分类' : '编辑设备位置分类'">
        <Form class="formList" ref="deviceForm" :model="formItem" :label-width="100" :rules="validateMalfunction">
            <FormItem label="类型：" prop="ftype">
                <Select v-model="formItem.ftype" filterable clearable :placeholder="`${$t('common.selectInfo')}`">
                    <Option v-for="(item) of typeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                </Select>
            </FormItem>
            <FormItem label="名称：" prop="name">
                <Input type="text" v-model="formItem.name" :placeholder="`${$t('common.inputInfo')}`" class="inputBorder"
                    :maxlength="100"></Input>
            </FormItem>
        </Form>
        <div slot="footer">
            <Button type="default" @click="handleCancel">{{ $t('common.Cancle') }}</Button>
            <Button type="primary" :loading="loading" @click="handleOk">{{ $t('maintenance.Confirmed') }}</Button>
        </div>
    </Modal>
</template>

<script>
export default {
    name: "deviceModal",
    data() {
        return {
            isAdd: false,
            toolMaintenanceVisible: false,
            loading: false,
            formItem: {
                ftype: '',
                name: '',
            },
            validateMalfunction: {
                ftype: [
                    { required: true, message: '请选择类型', trigger: 'change' }
                ],
                name: [
                    { required: true, message: '请输入名称', trigger: 'blur' }
                ],
            },
            typeList: [{ value: '1', label: '位置' }, { value: '2', label: '分类' }]
        }
    },
    methods: {
        show(type, record) {
            
            this.toolMaintenanceVisible = true
            if (type === 'add') {
                this.$refs.deviceForm.resetFields();
                this.isAdd = true
            } else {
                this.isAdd = false
                this.formItem = {
                    ...record,
                    // toolNum: String(record.toolNum)
                }
            }
        },
        handleOk() {
            let { id, ftype, name } = this.formItem
            this.loading = true
            this.$refs.deviceForm.validate((valid) => {
                if (valid) {
                    if (this.isAdd) {
                        let params = {
                            ftype,
                            name,
                        }
                        this.axios({
                            method: 'post',
                            url: '/dms/dcmAcctestClassify/addAddress',
                            data: params
                        }).then(res => {

                            if (res.code === 200) {
                                this.toolMaintenanceVisible = false
                                this.loading = false
                                this.$Message.success('新增成功！')
                                this.$refs.deviceForm.resetFields();
                                this.$emit('refresh')
                            } else {
                                this.toolMaintenanceVisible = false
                                this.loading = false
                                this.$Message.error(res.msg)
                            }
                        })
                    } else {
                        let params = {
                            id,
                            ftype,
                            name,
                        }
                        this.axios({
                            method: 'put',
                            url: '/workorder/workorderTool/edit',
                            data: params
                        }).then(res => {

                            if (res.code === 200) {
                                this.toolMaintenanceVisible = false
                                this.loading = false
                                this.$Message.success('编辑成功！')
                                this.$refs.deviceForm.resetFields();
                                this.$emit('refresh')
                            } else {
                                this.toolMaintenanceVisible = false
                                this.loading = false
                                this.$Message.error(res.msg)
                            }
                        })
                    }
                } else {
                    this.loading = false
                }
            })
        },
        handleCancel() {
            this.$refs.deviceForm.resetFields();
            this.toolMaintenanceVisible = false
        }
    }
}
</script>

<style scoped></style>
